AWS Lex 创建 Slack Bot - Integrating Lex Bot with Slack

非常简单的教程,新建一个 Slack Bot 支持购买房/车的功能。主要流程: 编写 Lambda Function => 调用 Lambda function 创建 Lex bot => 创建 Slack Application 并与 Lex bot 关联 => 测试并发布

Create Lambda Function

Lambda Function 部分的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
exports.handler = (event, context, callback) => {
var purchase = event.currentIntent.slots.purchase,
price = "free";
if (purchase === "home") {
price = "200000 dollars";
}
callback(null, {
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText",
"content": "You have purchased a " + event.currentIntent.slots.purchase + " for " + price
}
}
});
};

有一个 purchase 的 slot,这段代码返回的是,如果买房,价格 200000 刀,如果买其他东西(slot value 限制下),都免费。

Lambda 的配置不多说了,和 Alexa 开发新技能 - Lambda的配置相同。

供测试的 input event example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"currentIntent": {
"name": "PurchaseIntent",
"slots": {
"purchase": "home"
},
"confirmationStatus": "Confirmed"
},
"bot": {
"name": "bot-name",
"alias": "bot-alias",
"version": "bot-version"
},
"userId": "User ID specified in the POST request to Amazon Lex.",
"inputTranscript": "Text used to process the request",
"invocationSource": "FulfillmentCodeHook or DialogCodeHook",
"outputDialogMode": "Text or Voice, based on ContentType request header in runtime API request",
"messageVersion": "1.0",
"sessionAttributes": {
"key1": "value1",
"key2": "value2"
}
}

Expect Result:
expectRes.png

Create Amazon Lex Bot

登录 AWS Lex,新建一个 bot(get started => custom bot),过程非常简单,设置基本信息包括 bot name, output voice, session timeout 等,然后创建 slot type,设置 intent,基本流程和 Alexa add new skill (尤其是 Skill Builder Beta 界面)差不多,直接上截图了。

Create Lex Bot:
lex1.png
lex2.png

Add slot type:
AddSlot.png
保存的 slot type 在之后所有的 bot 设置中都可以重复使用。

Add intent:
提供一些 Sample utterances

1
2
3
I would like to purchase a {purchase}​
Buy me a {purchase}​
I'm going to buy a {purchase}​

lex3.png

Call lambda function:
lex4.png

如果用户输入了除 slot value (这里是 home, car)以外的东西,就会触发 Prompt

设置好之后选择 save intent,然后选右上角的 build,创建完成后就可以开始测试 bot,这里的测试结果有点不如人意,prompt 并没有起到作用,然而没关系,最后在 Slack 界面是 work 的
lex5.png

Create Slack Application

如果没有 Slack 账号,需要先注册注册
登录Slack API,选择 start building => create an app,然后进行相关设置:

  1. In the left menu, choose Bot Users.
    • Provide a user name.
    • For Always Show My Bot as Online, choose On.
      Save the changes.
  2. Choose Interactive Messages from the left menu.
    • Choose Enable Interactive Messages.
    • Specify any valid URL in the Request URL box. For example, you can use https://slack.com.
      Note
      For now, enter any valid URL so that you get the verification token that you need in the next step. You will update this URL after you add the bot channel association in the Amazon Lex console.
    • Choose Enable Interactive Messages.
  3. In the Settings section in the left menu, choose Basic Information. Record the following application credentials:
    • Client ID
    • Client Secret
    • Verification Token

Integrate the Slack Application with the Amazon Lex Bot

找到之前创建的 lex bot,到 Channels 标签页,在左边菜单栏选择 Slack,并提供下面的信息:

  • Type a name. For example, BotSlackIntegration.
  • Choose “aws/lex” from the KMS key drop-down.
  • For Alias, choose the bot alias.
  • Type the Client Id, Client secret, and Verification Token, which you recorded in the preceding step. These are the credentials of the Slack application.
Channels.png

关于 Aliases 的问题,可以在 SettingsAliases 部分进行设置,发现不设置 Alias 的话 Activate Bot 的时候可能会出错,所以还是设置下吧~
设置好后 Activate,记录下 Postback URLOAuth URL,然后回到 Slack Application 页面,做如下设置:

  1. Update the OAuth & Permissions feature as follows:
    1. In the Redirect URLs section, add the OAuth URL that Amazon Lex provided in the preceding step. Choose Add a new Redirect URL, and then choose Save URLs.
    2. In the Permission Scopes section, choose two permissions in the Select Permission Scopes drop down. Filter the list with the following text:
      • chat:write:bot
      • team:read
        Choose Save Changes.
  2. Update the Interactive Messages feature by updating the Request URL value to the Postback URL that Amazon Lex provided in the preceding step. Choose Add, and then choose Save URLs.
  3. Subscribe to the Event Subscriptions feature as follows:
    • Enable events by choosing the On option.
    • Set the Request URL value to the Postback URL that Amazon Lex provided in the preceding step.
    • Subscribe to the message.im bot event to enable direct messaging between the end user and the Slack bot.
    • Save the changes.

这里没什么 tricky 的地方,要注意的是每一步都要记得 Save

Test the Integration

  1. Choose Manage Distribution under Settings. Choose Add to Slack to install the application. Authorize the bot to respond to messsages.
  2. You are redirected to your Slack team. Choose your bot from the Direct Messages section in the left menu. If you don’t see your bot, choose the plus icon (+) next to Direct Messages to search for your bot.
  3. Engage in a chat with your Slack application, which is linked to the Amazon Lex bot. Your bot now responds to messages.Slack%20test.png

参考链接:
Integrating with Slack

徐阿衡 wechat
欢迎关注:徐阿衡的微信公众号
客官,打个赏呗~